﻿using Basic.FlowModel.AppItem;
using Basic.FlowModel.DB;
using Basic.FlowRemoteModel;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FlowDAL.lmpl
{
    internal class FlowAppItemDAL : BasicDAL<DBFlowAppItemList, long>, IFlowAppItemDAL
    {
        public FlowAppItemDAL ( IRepository<DBFlowAppItemList> basicDAL ) : base(basicDAL, "app.item")
        {
        }

        public long Add ( AppItemAdd data )
        {
            DBFlowAppItemList add = data.ConvertMap<AppItemAdd, DBFlowAppItemList>();
            add.Id = IdentityHelper.CreateId();
            add.Status = FlowItemAuditStatus.待提交;
            add.CreateTime = DateTime.Now;
            this._BasicDAL.Insert(add);
            return add.Id;
        }
        public void SetAppTitle ( long id, string title )
        {
            if ( !this._BasicDAL.Update(a => a.AppTitle == title, a => a.Id == id) )
            {
                throw new ErrorException("flow.app.item.title.set.fail");
            }
        }
        public void SetStatus ( long id, FlowItemAuditStatus status, DateTime? endTime )
        {
            if ( !this._BasicDAL.Update(new DBFlowAppItemList
            {
                Id = id,
                Status = status,
                EndTime = endTime
            }, new string[] { "Status", "EndTime" }) )
            {
                throw new ErrorException("flow.app.item.title.set.fail");
            }
        }
    }
}
